package com.jk.admin.controller;

import com.jk.admin.entity.Product;
import com.jk.admin.entity.Shoptype;
import com.jk.admin.service.ProductService;
import com.jk.admin.utils.HttpClientUtil;
import com.jk.admin.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;



    @RequestMapping("getListPro")
    public List<Product> getListPro(Product product){

        return productService.getListPro(product);

    }


    @RequestMapping("delPro")
    public void delPro(Integer id){
        productService.delPro(id);
    }

    @RequestMapping("updPro")
    public void updPro(Product product,HttpServletRequest request){
        String img = (String) request.getSession().getAttribute("img");
        product.setProduct_imgurl(img);
        System.out.println(img+"-----s----ss------s-----");
        productService.updPro(product);
    }

    @RequestMapping("getById")
    public Product huiPro(Integer id){

        return productService.huiPro(id);
    }

    @RequestMapping("addPro")
    public void addPro(Product product,HttpServletRequest request){
        String img = (String) request.getSession().getAttribute("img");
        product.setProduct_imgurl(img);
        System.out.println(img+"-----s----ss------s-----");
        productService.addPro(product);
    }

    @RequestMapping("upload")
    public Map upload(MultipartFile img, HttpServletRequest request) throws Exception{
        String upload2oss = OSSUtil.upload2oss(img);
        System.out.println(upload2oss);
        request.getSession().setAttribute("img", upload2oss);
        Map map=new HashMap<>();
        map.put("upload2oss", upload2oss);
        return map;
    }

    @RequestMapping("getecharts")
    public HashMap<String,Product> getecharts(){

        return productService.getecharts();

    }

    @RequestMapping("getListPros")
    public List<Product> getListPros(){

        return productService.getListPros();

    }
    @RequestMapping("addType")
    public void addType(Shoptype shoptype, HttpServletRequest request){
        String img = (String) request.getSession().getAttribute("img");
        shoptype.setS_img(img);
        System.out.println(img+"-----s----ss------s-----");
        productService.addType(shoptype);
    }
    @RequestMapping("upload123")
    public Map upload123(MultipartFile img, HttpServletRequest request) throws Exception{
        String upload2oss = OSSUtil.upload2oss(img);
        System.out.println(upload2oss);
        request.getSession().setAttribute("img", upload2oss);
        Map map=new HashMap<>();
        map.put("upload2oss", upload2oss);
        return map;
    }

    @RequestMapping("tong")
    public void tong(){
        String s = HttpClientUtil.doGet("http://127.0.0.1:8088/tong");
    }
}
